import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:hkzf_flutter/utils/image_cache_manager.dart';

// 网络图片的正则匹配
final netWorkUriRep = RegExp('^http');
// 本地图片的正则匹配
final localUriRep = RegExp('^static');

class CommonImage extends StatelessWidget {
  final String? src;
  final double? width;
  final double? height;
  final BoxFit? fit;
  const CommonImage({super.key, this.src, this.width, this.height, this.fit});

  @override
  Widget build(BuildContext context) {
    if(netWorkUriRep.hasMatch(src!)){// 网络图片处理
      return CachedNetworkImage(
        width: width, 
        height: height, 
        fit: fit,
        imageUrl: src!,
        cacheManager:ImageCacheManager().instance,// 使用自定义图片缓存策略
        placeholder: (context, url) => const Center(child: CircularProgressIndicator()),
        errorWidget: (context, url, error) => const Icon(Icons.error),
      );
    }
    if(localUriRep.hasMatch(src!)){// 本地图片处理
      return Image.asset(
        src!, 
        width: width, 
        height: height, 
        fit: fit,
      );

    }
    assert(false, '图片地址不合法');
    return Container();
  }
}